$(document).ready(function () {
		/* 封闭作用域 */
		var App = new Vue({
			el: "#vue-contaniner",
			data: function () {
				return {
					/* 当前订单数量 1 2 7 6 3 4 5*/
					orderName: [
						{name:'总订单',index:"1"},
						{name:'待发货',index:"2"},
						{name:'待续租',index:"7"}, 
						{name:'待归还',index:"6"}, 
						{name:'待结算',index:"3"}, 
						{name:'待退货',index:"4"}, 
						{name:'成功订单',index:"5"}],
					orderNumber: [],
					data: {
						goods_max_count: 0, //每笔限租
						is_cert: 1, //是否实名认证
						services: [2], //服务标 （数组）
						rent_period_types: {}, //租赁周期 （数组）
						express_model_id: -1, //快递模板ID
						todoor_model_id: -1, //上门模板ID
						since_sites: [], //自提点ID（多个ID用英文逗号拼接）
						return_addrs: [], //归还地址
					}, //首页数据
					freight: [], //快递模板
					freight_door: [], //上门模板
					freight_person: [], //自提模板
					sinceList: [], //归还地址
					currentData: {}, //当前选择数据
					/* 是否为第一次设置数据 0为否，1为是*/
					firstSetting:0,
					/* 当前数据类型 0:预览状态 1：修改状态*/
					dataType:0,
					/* 服务标数据列表 */
					serviceList: [{
						name: "免押金",
						click: 0,
						msg: "消费者下单时不需支付商品押金"
					}, {
						name: "包邮",
						click: 0,
						msg: "商品支持选择区域内，寄送的快递费由商家承担，归还的快递费由消费者承担"
					}, {
						name: "免赔",
						click: 0,
						msg: "物品轻微损坏无需赔偿，轻微损坏范围见商家说明"
					}, {
						name: "上门",
						click: 0,
						msg: "商家负责送货上门，同时负责归还上门取货"
					}, {
						name: "自提",
						click: 0,
						msg: "消费者可到商家设置的自提点，进行取货和归还"
					}, {
						name: "随租随还",
						click: 0,
						msg: "租赁的商品提前归还时，只收取实际租赁时间的租金,退还剩余租金"
					}, ],
					/* 周期表格说明 */
					dateInfo:[
						"用户需起租的最小周期",
						"用户可起租的最大周期",
						"用户最少在起租日前多少天可以下单租赁",
						"用户最多在起租日前多少月可以下单租赁",
						"用户续期租赁时租价的折扣",
					],
					/* 周期map表 */
					dateMap:["日","周","月","季","年"],
					/* 周期数据列表 */
					dateList: [{
						name: "日",
						click: 1,
						type: "day",
						data: [5, -1, 5, 12,100]
					}, {
						name: "周",
						click: 1,
						type: "week",
						data: [1, -1, 5, 12,100]
					}, {
						name: "月",
						click: 1,
						type: "month",
						data: [1, -1, 5, 12,100]
					}, {
						name: "季",
						click: 0,
						type: "season",
						data: [1, -1, 5, 12,100]
					}, {
						name: "年",
						click: 0,
						type: "year",
						data: [1, -1, 5, 12,100]
					}]
				}
			},
			computed: {
				/* 全选计算属性 */
				allChecks() {
					let self = this;
					let obj = [];
					if (self.freight_person.length <= self.data.since_sites.length) {
						obj[0] = "since";
					} else {
						obj[0] = "";
					}
					if (self.sinceList.length <= self.data.return_addrs.length) {
						obj[1] = "return";
					} else {
						obj[1] = "";
					}
					return obj;
				},
				/* 显示判断，前提条件，处于预览模式下 */
				/* 服务标是否显示，主要判断当前需要显示的三个服务标 */
				serviceShow(){
					if(this.serviceList[0].click!=1&&this.serviceList[2].click!=1&&this.serviceList[5].click!=1){
						return false;
					}else{
						return true;
					}
				},
				/* 周期设置是否显示，判断click */
				dateShow(){
					let index=0;
					for(let item of this.dateList) {
					  if(item.click==1){
						index++;
					  }
					}
					if(index==0){
						return false;
					}else{
						return true;
					}
					
				},
				/* 运费是否显示，三个数据合并判断*/
				freightShow(){
					/* 快递运费模板 */
					/* 自提运费模板 */
					/* 上门运费模板 */
					/* express_model_id: -1, //快递模板ID
					todoor_model_id: -1, //上门模板ID
					since_sites: [] */
					if(this.data.express_model_id==-1&&this.data.todoor_model_id==-1&&this.data.since_sites.length==0){
						return false;
					}else{
						return true;
					}
				},
			},
			methods: {
				/* 开始设置基本属性 */
				startChange(){
					this.dataType=1;
				},
				/* 初次进入显示设置 */
				showSetting(){
					this.dataType=1;
				},
				/* 取消编辑状态 */
				cancelSetting(){
					this.dataType=0;
				},
				/* 0 为 自提 1为 归还 */
				allCheck(type) {
					var self = this;
					if (type == 0) {
						if (self.allChecks[0] == "since") {
							self.data.since_sites = [];
						} else {
							self.data.since_sites = [];
							for (let item of self.freight_person) {
								self.data.since_sites.push(item.since_id);
							}

						}
					} else {
						if (self.allChecks[1] == "return") {
							self.data.return_addrs = [];
						} else {
							self.data.return_addrs = [];
							for (let item of self.sinceList) {
								self.data.return_addrs.push(item.revert_id);
							}
						}
					}
				},
				/* 最大租赁数量验证 */
				max:function(value){
					var self=this;
					setTimeout(function() {
						if(self.data.goods_max_count===NaN){
							return false;	
						}
						if(self.data.goods_max_count<0){
							self.data.goods_max_count=0;
						}
						else if(self.data.goods_max_count>50){
							self.data.goods_max_count=50;
						}else{
							self.data.goods_max_count=(self.data.goods_max_count-0).toFixed(0);	
						}
						var data=self.data.goods_max_count;
						self.data.goods_max_count=null;
						self.data.goods_max_count=data;
					}, 100);
				},/*  */
				percent:function(value){
					var self=this;
					setTimeout(function() {
						if(value===NaN){
							return false;	
						}
						if(value[4]<0){
							value[4]=0;
						}
						else if(value[4]>100){
							value[4]=100;
						}else{
							value[4]=(value[4]-0).toFixed(0);	
						}
						var data=self.dateList;
						self.dateList=[];
						self.dateList=data;
					}, 100);
				},
				dataMin(item, index) {
					item[index] = 0;
					let obj = this.dateList
					this.dateList = [];
					this.dateList = obj;
				},
				/* 最大租用周器无限期切换 */
				dataMax(item, index) {
					item[index] = -1;
					let obj = this.dateList
					this.dateList = [];
					this.dateList = obj;
				},
				/* 服务标选择 */
				serviceClick(index) {
					if (index != 2) {
						this.serviceList[index - 1].click = 1 - this.serviceList[index - 1].click;
					} else {
						layer.msg("包邮为必选属性");
					}
				},
				/* 日期选择 */
				dataClick(item) {
					if (item.click == 1) {
						item.click = 0;
					} else {
						/* 最多只能选择三个 */
						let flag = 0;
						for (let ite of this.dateList) {
							if (ite.click == 1) {
								flag++;
							}
						}
						if (flag == 3) {
							layer.msg("时间周期最多只能选择三个");
						} else {
							item.click = 1;
						}
					}
				},
				/* 获取订单数量 */
				getOrderNumber: function () {
					let self = this;
					$.ajax({
						url: baseUrl + "api/index/getOrderCount",
						type: "GET",
						data: {},
						async: true,
						dataType: "json",
						success: function (response) {
							let order=new Array(7);
							/* 1，2，7，6，3，4，5 */
							for(let num=0;num<order.length;num++) {
								if(num==6|| num==4 || num==5){
									order[num]=response.data[num-1];
								  }else if(num==2){
									order[num]=response.data[7];
								  }else if(num==3){
									order[num]=response.data[6];
								  }else{
									order[num]=response.data[num+1];
								  }	
							}
							self.orderNumber = order;
						},
						error: function (error) {

						}
					});
				},
				/* 获取首页常规数据 */
				getMainData() {
					/* var data=new Date().getTime(); */
					let self = this;
					$.ajax({
						url: baseUrl + "api/goods/getCommonAttr",
						type: "GET",
						data: {},
						async: true,
						dataType: "json",
						success: function (response) {
							/* 测试接口返回速度 */
							/* console.log(new Date().getTime()-data); */
							/* 首次完全没有数据 */
							if (response.status == 1) {
								self.data = response.data;
								self.dataRender(self.data);
							}else{
								self.firstSetting=1;
							}
						},
						error: function (error) {

						}
					});
				},
				/* 当前订单数据渲染 */
				dataRender(data) {
					/* 日期格式化 */
					for(let item of this.dateList) {
						item.click = 0;
					}
					if ( JSON.stringify(data.rent_period_types)!="{}") {
						for (let item in data.rent_period_types) {
							for (let ite of this.dateList) {
								if (ite.type == item) {
									ite.click = 1;
									ite.data = data.rent_period_types[item];
								}
							}
						}
					}
					/* 服务格式化 */
					if (data.services.length > 0) {
						for (let item of data.services) {
							this.serviceList[item - 1].click = 1;
						}
					}
					/* 兼容后端错误数据 */
					if(!data.since_sites){
						data.since_sites=[];	
					}
					if(!data.return_addrs){
						data.return_addrs=[];
					}
				},
				/* 获取快递模板 */
				getFreight() {
					let self = this;
					$.ajax({
						url: baseUrl + "merchant/goods/refresh_kd",
						type: "GET",
						data: {},
						async: true,
						dataType: "json",
						success: function (response) {
							if(response.status==1){
								/*首先进行数据验证 */
								let trueData=-1;
								for(let ite of response.data) {
									if(self.data.express_model_id==ite.freight_id){
										trueData=ite.freight_id;
									}
								}
								self.data.express_model_id=trueData;
								self.freight = response.data;
							}
						},
						error: function (error) {

						}
					});
				},
				/* 获取上门快递模板 */
				getFreightDoor() {
					let self = this;
					$.ajax({
						url: baseUrl + "merchant/goods/refresh_sm",
						type: "GET",
						data: {},
						async: true,
						dataType: "json",
						success: function (response) {
							if(response.status==1){
								/*首先进行数据验证 */
								let trueData=-1;
								for(let ite of response.data) {
									if(self.data.todoor_model_id==ite.freight_id){
										trueData=ite.freight_id;
									}
								}
								self.data.todoor_model_id=trueData;
								self.freight_door = response.data;
								self.getFreightPerson();
								self.getSince();
								}
						},
						error: function (error) {

						}
					});
				},
				/* 获取自提快递模板 */
				getFreightPerson() {
					let self = this;
					$.ajax({
						url: baseUrl + "api/goods/getSinces",
						type: "GET",
						data: {},
						async: true,
						dataType: "json",
						success: function (response) {
							/* staus为1说明有数据 */
							if(response.status==1){
								/*首先进行数据验证 */
								let trueData=[];
								for(let item of self.data.since_sites) {
									for(let ite of response.data) {
										if(item==ite.since_id){
											trueData.push(item);
										}
									}
									
								}
								self.data.since_sites=trueData;
								self.freight_person = response.data;	
							}
							
						},
						error: function (error) {

						}
					});
				},
				/* 获取归还模板 */
				getSince() {
					let self = this;
					$.ajax({
						url: baseUrl + "merchant/revert/getrevert",
						type: "GET",
						data: {},
						async: true,
						dataType: "json",
						success: function (response) {
							if(response.status==1){
								/*首先进行数据验证 */
								let trueData=[];
								for(let item of self.data.return_addrs) {
									for(let ite of response.data) {
										if(item==ite.revert_id){
											trueData.push(item);
										}
									}
									
								}
								self.data.return_addrs=trueData;
								self.sinceList = response.data;
							}
						},
						error: function (error) {

						}
					});
				},
				/* 数据保存 */
				saveData() {
					/* 在进行数据之前，应该判断当前数据是否全部为空，全为空的情况下是否将其状态设置为初次进入 */
					/* 目前方法为不判断，由后端决定用户状态 */
					let self = this;
					let dateObj = {};
					for (let item of this.dateList) {
						if (item.click == 1) {
							dateObj[item.type] = item.data;
						}
					}
					/* 服务标数据拼接 */
					let serObj = [];
					for (let item = 0; item < this.serviceList.length; item++) {
						if (this.serviceList[item].click == 1) {
							serObj.push(item + 1);
						}
					}
					/* 周期数据string转int */
					for (var key in dateObj) {
						for (let item in dateObj[key]) {
							dateObj[key][item] -= 0;
						}

					}
					this.data.rent_period_types = dateObj;
					this.data.services = serObj;
					$.ajax({
						url: baseUrl + "api/goods/saveCommonAttr",
						type: "POST",
						data: {
							data:JSON.stringify(self.data)
						},
						async: true,
						dataType: "json",
						success: function (response) {
							if (response.status == 1) {
								layer.msg("保存成功");
								window.location.reload();
							} else {
								layer.msg("保存失败，请重新尝试");
							}
						},
						error: function (error) {

						}
					});
				}
			},
			mounted: function () {
				this.getOrderNumber();
				this.getMainData();
				this.getFreight();
				this.getFreightDoor();
			}
		})

		$(".index_main_typeList").on("mouseover", ".index_main_typeSingle", function () {
			var obj = $(this);
			var msg = $(this).data("msg");
			layer.tips(msg, obj, {
				time: 100000,
				tips: [1, '#2196f3'] //还可配置颜色
			});
		});
		$(".index_main_typeList").on("mouseout", ".index_main_typeSingle", function () {
			layer.closeAll();
		});

		$(".index_main_dateEdit ").on("mouseover","h3", function () {
			var obj = $(this);
			var msg = $(this).data("msg");
			layer.tips(msg, obj, {
				time: 100000,
				tips: [1, '#2196f3'] //还可配置颜色
			});
		});
		$(".index_main_dateEdit ").on("mouseout","h3",  function () {
			layer.closeAll();
		});
		/* 查找订单月份 */
		let currentMonth,currentYear;
		currentMonth=new Date().getMonth()+1;
		currentYear=new Date().getFullYear();
		$(".currentMonth option").each(function(){
			if($(this).val()>=currentMonth){
				$(this).hide();	
			}
			if($(this).val()==currentMonth-1){
				$(this).attr("selected","selected");	
			}
			
		})
		$(".currentYear option").each(function(){
			console.log(currentYear);
			if($(this).val()==currentYear){
				$(this).attr("selected","selected");
			}
		})
	});